package com.yljk.yljkproject.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;

import com.alsk.rn.kit.RNKITTools;
import com.google.gson.Gson;
import com.yilijk.base.utils.ALog;

import java.io.IOException;
import java.util.HashMap;
/**
 * Create By shicheng
 *
 * 网络状态监听
 * network status listener broadcast
 *
 */
public class NetWorkStateReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HashMap<String, Object> hashMap = new HashMap<>();
                //检测API是不是小于23，因为到了API23之后getNetworkInfo(int networkType)方法被弃用
                hashMap.put("tag","internet");
                if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {
                    //获得ConnectivityManager对象
                    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

                    //获取ConnectivityManager对象对应的NetworkInfo对象
                    //获取WIFI连接的信息
                    NetworkInfo wifiNetworkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                    //获取移动数据连接的信息
                    NetworkInfo dataNetworkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
                    if (wifiNetworkInfo.isConnected() && dataNetworkInfo.isConnected()) {
//                Toast.makeText(context, "WIFI已连接,移动数据已连接", Toast.LENGTH_SHORT).show();

                        hashMap.put("WIFI",true);
                        hashMap.put("Moblie",true);
                        hashMap.put("info","WIFI&Mob is connect");
                    } else if (wifiNetworkInfo.isConnected() && !dataNetworkInfo.isConnected()) {
//                Toast.makeText(context, "WIFI已连接,移动数据已断开", Toast.LENGTH_SHORT).show();
                        hashMap.put("WIFI",true);
                        hashMap.put("Moblie",false);
                        hashMap.put("info","WIFI is connect，Mob is disconnect");
                    } else if (!wifiNetworkInfo.isConnected() && dataNetworkInfo.isConnected()) {
//                Toast.makeText(context, "WIFI已断开,移动数据已连接", Toast.LENGTH_SHORT).show();
                        hashMap.put("WIFI",false);
                        hashMap.put("Moblie",true);
                        hashMap.put("info","WIFI is disconnect，Mob is connect");
                    } else {
//                Toast.makeText(context, "WIFI已断开,移动数据已断开", Toast.LENGTH_SHORT).show();
                        hashMap.put("WIFI",false);
                        hashMap.put("Moblie",false);
                        hashMap.put("info","WIFI is disconnect，Mob is disconnect");
                    }
//API大于23时使用下面的方式进行网络监听
                }else {
                    System.out.println("API level 大于23");
                    //获得ConnectivityManager对象
                    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

                    //获取所有网络连接的信息
                    Network[] networks = connMgr.getAllNetworks();
                    //用于存放网络连接信息
                    StringBuilder sb = new StringBuilder();
                    //通过循环将网络信息逐个取出来
                    for (int i=0; i < networks.length; i++){
                        //获取ConnectivityManager对象对应的NetworkInfo对象
                        NetworkInfo networkInfo = connMgr.getNetworkInfo(networks[i]);
                        sb.append(networkInfo.getTypeName() + " connect is " + networkInfo.isConnected());
//                hashMap.put(networkInfo.getTypeName(),networkInfo.isConnected());
                    }
                    if (networks.length == 0){
                        hashMap.put("WIFI",false);
                        hashMap.put("Moblie",false);
                        hashMap.put("info","WIFI is disconnect，Mob is disconnect");

                    }else{
                        for (int i=0; i < networks.length; i++){
                            NetworkInfo networkInfo = connMgr.getNetworkInfo(networks[i]);
                            if (networkInfo != null) {
                                hashMap.put(networkInfo.getTypeName(), networkInfo.isConnected());
                            }
                        }
                        hashMap.put("info",sb);
                    }
                }
                if (isNetworkOnline()){
                    hashMap.put("msg","当前网络连接正常");
                    hashMap.put("isResult",true);
                }else{
                    hashMap.put("msg","当前网络不可用");
                    hashMap.put("isResult",false);
                }
                RNKITTools.INSTANCE.sendBroadcast(context, "ReceiveRefreshKey", new Gson().toJson(hashMap));

            }
        }).start();
    }

    public boolean isNetworkOnline() {
        Runtime runtime = Runtime.getRuntime();
        try {
            Process ipProcess = runtime.exec("ping -c 3 www.baidu.com");
            int exitValue = ipProcess.waitFor();
            ALog.i("Avalible", "Process:"+exitValue);
            return (exitValue == 0);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
        return false;
    }
}
